package com.baomidou.mybatisplus.criterion;

import java.util.ArrayList;
import java.util.List;

/**
 * 查询排序
 * @author Timothy Ren
 */
public class QueryOrder {

	/** 排序字段集合 */
	private List<OneOrder> oneOrders = new ArrayList<OneOrder>();

	public QueryOrder() {
	}

	public QueryOrder(String propertyName, QueryOrderMode orderMode) {
		oneOrders.add(new OneOrder(propertyName, orderMode));
	}

	public QueryOrder add(String propertyName, QueryOrderMode orderMode) {
		oneOrders.add(new OneOrder(propertyName, orderMode));
		return this;
	}

	public boolean hasOrder() {
		if (oneOrders.size() == 0) {
			return false;
		} else {
			return true;
		}
	}

	public String toSqlString() {
		if (oneOrders.size() == 0) {
			return "";
		} else {
			String queryOrder = "";
			for (int i = 0; i < oneOrders.size(); i++) {
				OneOrder order = (OneOrder) oneOrders.get(i);
				if (i == 0) {
					queryOrder = order.toSqlString();
				} else {
					queryOrder += "," + order.toSqlString();
				}
			}
			return queryOrder;
		}
	}

	/**
	 * 排序字段
	 */
	private class OneOrder {

		private String propertyName;
		private QueryOrderMode orderMode;

		public OneOrder(String propertyName, QueryOrderMode orderMode) {
			this.propertyName = propertyName;
			this.orderMode = orderMode;
		}

		public String getPropertyName() {
			return propertyName;
		}

		public QueryOrderMode getOrderMode() {
			return orderMode;
		}

		public String toSqlString() {
			StringBuffer fragment = new StringBuffer();
			fragment.append(propertyName).append(" ").append(orderMode.getOrderStr());
			return fragment.toString();
		}
	}
}
